import 'package:enjoy_plus_hm/pages/Home/Detail.dart';
import 'package:enjoy_plus_hm/pages/Login/index.dart';
import 'package:enjoy_plus_hm/pages/Profile/index.dart';
import 'package:enjoy_plus_hm/pages/tab_bar/index.dart';
import 'package:enjoy_plus_hm/utils/token.dart';
import 'package:flutter/material.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized(); // 必须在使用插件前调用
  runApp(
    MaterialApp(
      routes: {
        '/': (context) => TabBarPage(),
        '/detail': (context) => NoticeDetail(),
        '/login': (context) => LoginPage(),
      },
      onGenerateRoute: (setting) {
        // 获取token
        final token = TokenManager().getToken() ?? '';
        if (token.isEmpty && setting.name != '/login') {
          return MaterialPageRoute(builder: (context) => LoginPage());
        }
        if (setting.name == '/profile') {
          final userInfo = setting.arguments as Map;
          return MaterialPageRoute(
            builder: (context) => ProfilePage(userInfo: userInfo),
          );
        }
        return null;
      },
      initialRoute: '/',
    ),
  );
}
